package com.yiba.vpn.util;


import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;



public class SendEmail {

     public static final String HOST = "smtp.163.com";
    //public static final String HOST = "smtp.ym.163.com";
    public static final String PROTOCOL = "smtp";
    public static final int PORT = 25;
    public static final String FROM = "zhangws1229@163.com";//发件人的email
    public static final String PWD = "zwsok16789";//发件人密码
    //public static final String FROM = "support@shadows.rocks";//发件人的email
    //public static final String PWD = "7MIVa27C3a";//发件人密码
    public static final String personal = "壁虎VPN";//发件人密码

    /**
     * 获取Session
     * @return
     */
    private static Session getSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);//设置服务器地址
        props.put("mail.store.protocol" , PROTOCOL);//设置协议
        props.put("mail.smtp.port", PORT);//设置端口
        props.put("mail.smtp.auth" , true);

        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM, PWD);
            }

        };
        Session session = Session.getDefaultInstance(props , authenticator);

        return session;
    }

    public static void send(String toEmail , String content) throws UnsupportedEncodingException {
        Session session = getSession();
        try {
            System.out.println("--send--"+content);
            // Instantiate a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            //msg.setFrom(new InternetAddress(FROM));
            msg.setFrom(new InternetAddress(FROM, personal));
            InternetAddress[] address = {new InternetAddress(toEmail)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(personal);
            msg.setSentDate(new Date());
            msg.setContent(content , "text/html;charset=utf-8");

            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    public static void sendActivety(String toemail,String validatecode) throws UnsupportedEncodingException {

        StringBuffer sb=new StringBuffer("点击下面链接，48小时生效，否则重新注册账号，链接只能使用一次，请尽快！</br>");
        sb.append("<a href=\"http://localhost:8080/user/activate?action=activate&email=");
        sb.append(toemail);
        sb.append("&validateCode=");
        sb.append(validatecode);
        sb.append("\">xx！！");
        sb.append("</a>");
        //发送邮件
        SendEmail.send(toemail, sb.toString());
    }

    public static void sendPwd(String toemail,String pwd) throws UnsupportedEncodingException {

        StringBuffer sb=new StringBuffer("你的密码是 ！</br>");
        sb.append(pwd);
        //发送邮件
        SendEmail.send(toemail, sb.toString());
    }

    public static void main(String[] args) {
        ///邮件的内容
        //String  toemail = "sunkai@51master.tv";
        String  toemail = "xubodhd123@gmail.com";

        String validatecode = "22";
        StringBuffer sb=new StringBuffer("xx bug点击下面链接激活账号，48小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>");
        sb.append("<a href=\"http://localhost:8080/user/activate?email=");
        sb.append(toemail);
        sb.append("&validateCode=");
        sb.append(validatecode);
      //  sb.append("\">http://localhost:8080/register/action?action=activate&email=");
      //  sb.append(toemail);
      //  sb.append("&validateCode=");
      //  sb.append(validatecode);
        sb.append("\">xxxx！！");
        sb.append("</a>");

        //发送邮件
        try {
            SendEmail.send(toemail, sb.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("发送邮件");


    }

}